package com.staginfo.segs.util;

import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/7/12 0012.
 */
public class UuidUtils {
    /**
     * 根据手机的IMEI值生成唯一的UUID
     * @param imei
     * @return
     */
    public static UUID fromString(String imei){
        StringBuilder sb = new StringBuilder();
        sb.append(imei.substring(0, 8) + "-");
        sb.append(imei.substring(8, 12) + "-");
        if (imei.length() <= 14) {
            sb.append(imei.substring(12, 14) + "00-");
            sb.append("0000-000000000000");
        } else if (imei.length() == 15) {
            sb.append(imei.substring(12, 15) + "0-");
            sb.append("0000-000000000000");
        } else if (imei.length() == 16) {
            sb.append(imei.substring(12, 16) + "-");
            sb.append("0000-000000000000");
        } else if (imei.length() == 17) {
            sb.append(imei.substring(12, 16) + "-");
            sb.append(imei.substring(16, 17) + "000-000000000000");
        }
        UUID uuid = UUID.fromString(sb.toString());
       // LogUtils.d("hehe","imei:"+uuid.toString());
        return uuid;
    }

    public static String toUUIDString(String idStr){
        if (idStr==null || idStr.length()!=32)
            return null;
        StringBuffer buffer = new StringBuffer();
        buffer.append(idStr.substring(0, 8) + "-");
        buffer.append(idStr.substring(8, 12) + "-");
        buffer.append(idStr.substring(12, 16) + "-");
        buffer.append(idStr.substring(16, 20) + "-");
        buffer.append(idStr.substring(20));
        return buffer.toString();
    }

    /**
     * 将List<UUID>转为string
     * @param uuids
     * @return
     */
    public static String uuidsToString(List<UUID> uuids){
        int size=uuids.size();
        StringBuffer buf=new StringBuffer();
        for(int i=0;i<size;i++){
            buf.append(uuids.get(i).toString());
            if(i!=size-1){
                buf.append(",");
            }
        }
        return buf.toString();
    }
}
